package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BaseParamListBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.admin.mall.bo.BrandBaseBo;
import com.cskaoyan.bean.admin.mall.bo.BrandCreateBo;
import com.cskaoyan.bean.admin.mall.bo.BrandUpdateBo;
import com.cskaoyan.bean.admin.mall.vo.BrandCreateVo;
import com.cskaoyan.bean.admin.mall.vo.BrandUpdateVo;
import com.cskaoyan.bean.pojo.Brand;
import com.cskaoyan.bean.pojo.BrandExample;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.service.admin.BrandService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @program: project-two
 * @description:
 * @author: YuHui
 * @create: 2021-08-13 16:27
 **/
@Service
@Transactional
public class BrandServiceImpl implements BrandService {


    @Autowired
    BrandMapper brandMapper;


    /**
     * 品牌制造商的更新
     * @param brandUpdateBo
     * @return
     */


    @Override
    public BrandUpdateVo update(BrandUpdateBo brandUpdateBo) {

        Brand brand = new Brand();
        brand.setId(brandUpdateBo.getId());
        brand.setName(brandUpdateBo.getName());
        brand.setDesc(brandUpdateBo.getDesc());
        brand.setPicUrl(brandUpdateBo.getPicUrl());
        brand.setSortOrder(brandUpdateBo.getSortOrder());
        brand.setFloorPrice(brandUpdateBo.getFloorPrice());
        brand.setAddTime(brandUpdateBo.getAddTime());
        brand.setUpdateTime(brandUpdateBo.getUpdateTime());
        brand.setDeleted(false);
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();

        criteria.andIdEqualTo(brandUpdateBo.getId());
        brandMapper.updateByExample(brand, brandExample);
        Brand mallBrand1 = brandMapper.selectByPrimaryKey(brandUpdateBo.getId());
        //回显的时候deleted设置为false
        BrandUpdateVo brandUpdateVo = new BrandUpdateVo(mallBrand1.getId(), mallBrand1.getName(), mallBrand1.getDesc(), mallBrand1.getPicUrl(), mallBrand1.getSortOrder(), mallBrand1.getFloorPrice(), mallBrand1.getAddTime(), mallBrand1.getUpdateTime(), false);

        return brandUpdateVo;
}

    /**
     * 品牌制造商的刷新
     * @param baseParmList
     * @return
     */
    @Override
    public BaseRespData<Brand> list(BaseParamListBo baseParmList) {

        //分页
        PageHelper.startPage(baseParmList.getPage(), baseParmList.getLimit());
        String id = baseParmList.getId();
        String name = baseParmList.getName();

        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (id != null && !"".equals(id)) {
            if(!id .matches("\\d+")){
                return null;
            }
            Integer id1 = Integer.valueOf(id);
            criteria.andIdEqualTo(id1);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        //排序
        brandExample.setOrderByClause(baseParmList.getSort() + " " + baseParmList.getOrder());
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        //求总数
        int total = (int) brandMapper.countByExample(brandExample);
        return BaseRespData.create(brands,total);
    }


    /**
     * 品牌制造商的新增
     * @param brandCreateBo
     * @return
     */
    @Override
    public BrandCreateVo create(BrandCreateBo brandCreateBo) {

        Brand brand = new Brand();
        brand.setName(brandCreateBo.getName());
        brand.setDesc(brandCreateBo.getDesc());
        brand.setFloorPrice(brandCreateBo.getFloorPrice());
        brand.setPicUrl(brandCreateBo.getPicUrl());

        //以当前时间为数据库数据加入时间
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        brand.setAddTime(timestamp);
        brand.setUpdateTime(timestamp);

        int i = brandMapper.insertSelective(brand);
        BrandCreateVo brandCreateVo = null;

        if(i == 1){
            Integer id = brand.getId();
            Brand brand1 = brandMapper.selectByPrimaryKey(id);
            brandCreateVo = new BrandCreateVo(brand1.getId(), brand1.getName(), brand1.getDesc(), brand1.getPicUrl()
                    , brand1.getFloorPrice(), brand1.getAddTime(), brand.getUpdateTime());

        }
        return brandCreateVo;
    }


    /**
     * 品牌制造商的删除
     * @param brandBaseBo
     */
    @Override
    public void delete( BrandBaseBo brandBaseBo) {

        Brand brand = new Brand();
        brand.setId(brandBaseBo.getId());
        brand.setName(brandBaseBo.getName());
        brand.setDesc(brandBaseBo.getDesc());
        brand.setPicUrl(brandBaseBo.getPicUrl());
        brand.setSortOrder(brandBaseBo.getSortOrder());
        brand.setFloorPrice(brandBaseBo.getFloorPrice());
        brand.setAddTime(brandBaseBo.getAddTime());
        brand.setUpdateTime(brandBaseBo.getUpdateTime());
        brand.setDeleted(true);

        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();

        criteria.andIdEqualTo(brandBaseBo.getId());

        brandMapper.updateByExample(brand,brandExample);
    }
}
